package cn.ming.wfchat.app.main;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;
import cn.ming.wfchat.app.AppService;
import cn.ming.wfchat.app.moment.third.beans.UserBean;
import cn.ming.wfchat.kit.group.GroupInfoActivity;
import cn.ming.wfchat.kit.user.UserInfoActivity;
import cn.ming.wfchat.kit.user.UserViewModel;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

public class ThirdLaunchActivity extends AppCompatActivity {

    private static final String TAG = "ThirdLaunchActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (!isOtherUIExisting(getApplicationContext())) {
//            startActivity(new Intent(ThirdLaunchActivity.this, SplashActivity.class));
//        }
        Uri uri = getIntent().getData();
        if (uri != null) {
            // 完整的url信息
            String url = uri.toString();
            Log.i(TAG, "url:" + uri);

            // scheme部分
            String scheme = uri.getScheme();
            Log.i(TAG, "scheme:" + scheme);

            // host部分
            String host = uri.getHost();
            Log.i(TAG, "host:" + host);

            // port部分
            int port = uri.getPort();
            Log.i(TAG, "port:" + port);

            // 访问路劲
            String path = uri.getPath();
            Log.i(TAG, "path:" + path);

            List<String> pathSegments = uri.getPathSegments();

            // Query部分
            String query = uri.getQuery();
            Log.i(TAG, "query:" + query);

            //获取指定参数值
            String success = uri.getQueryParameter("success");
            Log.i(TAG, "success:" + success);

            //String prefix = query.substring(0, query.lastIndexOf('=') + 1);
            String value = query.substring(query.lastIndexOf("=") + 1);
            if (path.equals("/user")) {
                showUser(value);
            } else if (path.equals("/group")) {
                joinGroup(value);
            } else {
                startActivity(new Intent(ThirdLaunchActivity.this, SplashActivity.class));
            }
        }

        finish();
    }

    /**
     * 判断是否有别的页面存在
     *
     * @param context
     * @return
     */
    private boolean isOtherUIExisting(Context context) {
        boolean existing = false;
        try {
            String currClassName = getClass().getName();
            String currPackageName = getPackageName();
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(10);
            if (list.size() <= 0) {
                existing = false;
            }
            for (ActivityManager.RunningTaskInfo info : list) {
                String activityName = info.baseActivity.getClassName();
                if (activityName.equals(currClassName)) {
                    continue;
                }
                if (info.baseActivity.getPackageName().equals(currPackageName)) {
                    existing = true;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            existing = false;
        }
        return existing;
    }

    private void showUser(String mobile) {
        AppService.Instance().getInfoByMobile(mobile, new AppService.InfoByMobileCallback() {
            @Override
            public void onUiSuccess(UserBean assetsBean) {
                UserViewModel userViewModel = ViewModelProviders.of(ThirdLaunchActivity.this).get(UserViewModel.class);
                UserInfo userInfo = userViewModel.getUserInfo(assetsBean.getUserId(), true);
                if (userInfo == null) {
                    return;
                }
                Intent intent = new Intent(ThirdLaunchActivity.this, UserInfoActivity.class);
                intent.putExtra("userInfo", userInfo);
                startActivity(intent);
            }

            @Override
            public void onUiFailure(int code, String msg) {
                ChatManager.showToast(msg);

            }
        });
    }

    private void joinGroup(String groupId) {
        Intent intent = new Intent(this, GroupInfoActivity.class);
        intent.putExtra("groupId", groupId);
        startActivity(intent);
    }

}
